<?php
	require_once('includes.php');
	include_once('check_login_function.php');

	//These functions are used in general forms which are specified
	
	//Used in profile.php
	//Validates the account if the correct username and token input
	if (isset($_POST['validateAccount'])) {
		$username = $user->getUsername();
		$token = $_POST['token'];
		//Validate the account
		if (User::validate($username, $token)) {
			$message[] = "MSG_ACCOUNT_VALID";
		} else {
			$message[] = "ERROR_TOKEN_INVALID";
		}
		$_SESSION['message'] = $message; 
		header('Location: ' . DIR . '/profile.php');
	}

	//Used in profile.php
	//Resends the validating token
	if (isset($_POST['resendToken'])) {
		//Send the e-mail token
		$user->sendEmailToken();
		$message[] = "MSG_TOKEN_SENT";
		$_SESSION['message'] = $message; 
		header('Location: ' . DIR . '/profile.php');
	}
	
	//Used in request.php
	//Remove the doctor request by the patient
	if (isset($_POST['removeDoctorRequest'])) {
		$doctor_id = $_POST['doctorID'];
		$_SESSION['saved'] = $doctor_id;
		//Remove request from the doctor
		$user->removeRequestFrom($doctor_id);
		$message[] = "MSG_REQUEST_REMOVED";
		$_SESSION['message'] = $message; 
		header('Location: ' . DIR . '/request.php');
	}
	
	//Used in request.php
	//Send the doctor request by the patient
	if (isset($_POST['sendDoctorRequest'])) {
		$doctor_id = $_POST['doctorID'];
		$_SESSION['saved'] = $doctor_id;
		$request_note = $_POST['note'];
		//Send the request to the doctor
		$user->sendRequestTo($doctor_id, $request_note);
		header('Location: ' . DIR . '/request.php');
	}
	
	//Used in patients.php
	//Remove patient request by the doctor
	if (isset($_POST['removePatientRequest'])) {
		$patient_id = $_POST['patientID'];
		//Remove request from the patient
		$user->removeRequestFrom($patient_id);
		$message[] = "MSG_REQUEST_REMOVED";
		$_SESSION['message'] = $message; 
		header('Location: ' . DIR . '/patients.php');
	} 	
	
	//Used in patients.php
	//Accept patient request by the doctor
	if (isset($_POST['acceptPatientRequest'])) {
		$patient_id = $_POST['patientID'];
		//Accept the request of the patient
		$user->acceptRequest($patient_id);
		$message[] = "MSG_REQUEST_ACCEPTED";
		$_SESSION['message'] = $message; 
		header('Location: ' . DIR . '/patients.php');
	}
	
	//Used in log_form.php
	//Create a new log to input
	if (isset($_POST['enterLog'])) {
		$user_id = $user->getUserID();
		//Check if the log value is not empty
		if (strlen($_POST['logValue']) !== 0) {	
			//Split the value and the unit from the entry
			$datalog_entry = preg_split('#(?<=\d)(?=[a-z])#i', preg_replace('/\s+/', '', $_POST['logValue']));
			$datalog_value = $datalog_entry[0];
			$datalog_unit = null;
			
			//If there is a unit, set the unit
			if (count($datalog_entry) === 2) {
				$datalog_unit = $datalog_entry[1];
			}
			$type_id = $_POST['logType'];
			
			//Check if datalog value is a numeric
			if (is_numeric($datalog_value)) {
				$datalog_note = $_POST['logNote'];
				$datalog_time = $_POST['logCustomTime'];
				
				//Check if "now" is selected
				if ($_POST['logTime'] === "now") {
					$datalog = new DataLog($user_id, $type_id, $datalog_value, $datalog_unit, $datalog_note);
				} else {
					$datalog = new DataLog($user_id, $type_id, $datalog_value, $datalog_unit, $datalog_note, $datalog_time);
				}
				$datalog_id = $datalog->insert();
				$message[] = "MSG_LOG_CREATED";
			} else {
				$message[] = "ERROR_LOG_INVALID";
			}
		} else {
			$message[] = "ERROR_LOG_EMPTY";
		}
		$_SESSION['message'] = $message; 
		header('Location: ' . DIR . '/' . $_POST['from']);
	}
	
	//Used in log.php
	//Remove the log
	if (isset($_POST['removeLog'])) {
		$datalog_id = intval($_POST['datalogID']);
		$datalog = DataLog::createDataLog($datalog_id);
		$datalog->remove();
		$message[] = "MSG_LOG_REMOVED";
		$_SESSION['message'] = $message; 
		header('Location: ' . DIR . '/' . $_POST['from']);
	}

	//Used in log.php
	//Set participation in benchmarking
	if (isset($_POST['enterBenchmark'])) {
		//Check if benchmark is set
		if (isset($_POST['benchmark'])) {
			$user_id = $user->setPatientBenchmark(true);
			$message[] = "MSG_BENCHMARK_PARTICIPATE";
		} else {
			$user_id = $user->setPatientBenchmark(false);
			$message[] = "MSG_BENCHMARK_UNPARTICIPATE";
		}
		$_SESSION['message'] = $message; 
		header('Location: ' . DIR . '/' . $_POST['from']);
	}
	
	
	//Used in food_form.php
	//Enter a new food diary entry
	if (isset($_POST['enterFood'])) {
		$user_id = $user->getUserID();
		//Check if food name and food calories are not empty
		if (strlen($_POST['foodName']) !== 0 && strlen($_POST['foodCalories']) !== 0) {
			//Check if food calories is numeric
			if (is_numeric($_POST['foodCalories'])) {
				$food_calories = $_POST['foodCalories'];
				$food_name = $_POST['foodName'];
				$food_note = $_POST['foodNote'];
				$food_time = $_POST['foodCustomTime'];
				//Check if "now" is selected
				if ($_POST['foodTime'] === "now") {
					$food = new Food($user_id, $food_name, $food_calories, $food_note);
				} else {
					$food = new Food($user_id, $food_name, $food_calories, $food_note, $food_time);
				}
				$food->insert();				
				$message[] = "MSG_FOOD_CREATED";
			} else {
				$message[] = "ERROR_FOOD_INVALID";
			}
		} else {
			$message[] = "ERROR_FOOD_EMPTY";
		}
		$_SESSION['message'] = $message; 
		header('Location: ' . DIR . '/food.php');
	}
	
	//Used in food.php
	//Remove food diary entry
	if (isset($_POST['removeFood'])) {
		$food_id = intval($_POST['foodID']);
		$food = Food::createFood($food_id);
		$food->remove();	
		$message[] = "MSG_FOOD_REMOVED";
		$_SESSION['message'] = $message; 
		header('Location: ' . DIR . '/food.php');
	}	
	
	//Used in symptom_form.php
	//Enter a new symptom 
	if (isset($_POST['enterSymptom'])) {
		$user_id = $user->getUserID();
		//Check if symptom name is not empty
		if (strlen($_POST['symptomName']) !== 0) {
			$symptom_name = $_POST['symptomName'];
			$symptom_level = $_POST['symptomLevel'];
			$symptom_note = $_POST['symptomNote'];
			$symptom_time = $_POST['symptomCustomTime'];
			
			//Check if "now" is selected
			if ($_POST['symptomTime'] === "now") {
				$symptom = new Symptom($user_id, $symptom_name, $symptom_level, $symptom_note);
			} else {
				$symptom = new Symptom($user_id, $symptom_name, $symptom_level, $symptom_note, $symptom_time);
			}
			$symptom_id = $symptom->insert();			
			$message[] = "MSG_SYMPTOM_CREATED";
		} else {
			$message[] = "ERROR_SYMPTOM_EMPTY";
		}
		$_SESSION['message'] = $message; 
		header('Location: ' . DIR . '/symptom.php');
	}
	
	//Used in symptom.php
	//Remove symptom 
	if (isset($_POST['removeSymptom'])) {
		$symptom_id = intval($_POST['symptomID']);
		$symptom = Symptom::createSymptom($symptom_id);
		$symptom->remove();
		
		$message[] = "MSG_SYMPTOM_REMOVED";
		$_SESSION['message'] = $message; 
		header('Location: ' . DIR . '/symptom.php');
	}	
	
?>